#!/usr/bin/python
# coding:utf-8
import re
import time
from urllib.request import urlopen

videoList = []

def getHtml(url):
    try:
        response = urlopen(url)
        text = response.read()
        return text
    except BaseException as arg:
        print(arg)
        return "error"

def getPornUrl(listItem):
    pat = re.compile(r"'720': 'https:[\s\S]{100,350}480'")
    url = pat.findall(listItem)
    return url

def makeHtml(list):
    htmlF = open("out.html","w",encoding="utf-8")
    index = 0
    htmlF.write("<h1>get " + str(len(list)) + " videos totally</h1>")
    for url in list:
        htmlF.write("<a href='" + url + "'>"+ str(index) +"</a></br>")
        index += 1
    htmlF.close()

if __name__ == '__main__':
        #frech = getHtml("http://66.42.44.102/frech.log").decode("utf-8")
        while 1==1:
            infile = open("frech.log","r")
            frech = infile.read()
            infile.close()
            for item in getPornUrl(frech):
                start = item.find("720")+7
                end = item.find("480")-4
                UrlStr = ""
                for i in range(start,end):
                    UrlStr += item[i]
                videoList.append(UrlStr)
            print("Get " + str(len(videoList))+ " videos totally")
            makeHtml(videoList)
            time.sleep(300)
